function dosubmit() {
    $("#error_message").css("display", "none");
    $.ajax({
        url : 'login!queryRandom.action?_=' + Date.parse(new Date())
    }).done(function(d) {
        if (d.ok) {
            var r = d.value;
            var p = $.md5($.md5c($("#passwordLogin").val(), $("#md5Time").val())+ r);
            var a = $("#accountLogin").val();
            $.ajax({
                url : "login!authentication.action",
                data : {
                    account : a,
                    password : p,
                    random : r
                }
            }).done(function(data) {
                if (data.ok) {
                    /*
                     * default_pager_size*
                     * 默认的列表分页长度
                     * userForRemember*
                     * 自动登录账号及密码
                     */
                    var default_pager_size = locache.get('default_pager_size');
                    locache.flush();
                    locache.set('default_pager_size',default_pager_size);

                    saveLoginUser(data.value);
                    var rememberMe = $('#rememberMe')[0].checked;
                    if (rememberMe) {
                        var userForRemember = {
                                "name" : $("#accountLogin").val(),
                                "password" : $("#passwordLogin").val()
                        };
                        locache.set('userForRemember',userForRemember);
                    } else {
                        locache.remove('userForRemember');
                    }
                    var url = 'main.action?' + Date.parse(new Date());
                    doNavigate(url);
                } else {
                    $("#error_message").html(data.message).css("display", "");
                }
            }).fail(function() {
                $("#error_message").html("服务器连接请求异常").css("display", "");
            });
        }
    }).fail(function() {
        $("#error_message").html("服务器连接请求异常").css("display", "");
    });
}

function doValidate() {
    $.validity.start();
    $("#name").require("请输入用户姓名").maxLength(50,"最长50");
    $("#accountRegister").require("请输入用户帐号").maxLength(50,"最长50");
    $("#passwordRegister").require("请输登录密码");
    $("#registerForm input[type='password']").equal('密码输入不一致');
    
    var result = $.validity.end();
    return result.valid;
}

$(function() {
    var userForRemember = locache.get('userForRemember');
    if (userForRemember) {
        $("#accountLogin").val(userForRemember.name);
        $("#passwordLogin").val(userForRemember.password);
        $('#rememberMe').attr('checked', true);
    }
    
    $('#registerForm').submit(function(){
        if(doValidate()){
            $('input[type="submit"]').attr("disabled","disabled");
            $("#passwordForRegister").val($.md5c($("#passwordRegister").val(), $("#md5Time").val()));
            $("#passwordRegister").removeAttr('name');
            $("#passwordRegisterAgain").removeAttr('name');
            
            $.ajax({
                type: 'POST',
                url: $('#registerForm').attr('action'),
                data: $('#registerForm').serialize(),
                success: function(data){
                    if(data.ok){
                        showMessage('用户创建成功', function() {
                            show_div('reDiv', 'loginDiv');
                        });
                    } else {
                        showMessage('用户已存在,创建失败：' + data.message, null,'error');
                    }
                },
                complete:function() {
                    $('input[type="submit"]').removeAttr("disabled");
                }
            });
        }
        return false;
    });

    $("#unitNumber").keypress(function(e) {
        if (e.keyCode == 13) {
            $("#account").focus();
        }
    });
    $("#account").keypress(function(e) {
        if (e.keyCode == 13) {
            $("#password").focus();
        }
    });
    $("#password").keypress(function(e) {
        if (e.keyCode == 13) {
            dosubmit();
        }
    });
    $("#unitNumber").focus();
    $("#reDiv").addClass('hide');
});

function show_div(id1, id2) {
    $('#' + id1).addClass('hide');
    $('#' + id2).removeClass('hide');
}